<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_events extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		// Do something here
		return true;
		
	}
	
	/**
	 * This is intended to load data from database
	 *
	 * @param integer $id identifier of the object loaded
	 * @return boolean
	 */
	public function create_event_type($data)
	{
		$query = "insert into event_type (event_type_name,category_name,created_date,updated_date) values ('".$data['event_type_name']."','".$data['category_name']."','".date("Y-m-d h:i:s")."','".date("Y-m_d h:i:s")."')";
			$result = $this->conn->dbCommand($query);
			if($result)
			{
				return array('message'=> 'Successfully Created');
			}else 
			{
				return array('message'=>'Creation Failed');
			}
		
	}
	public function edit_event_type($data)
	{	
	$query="update `event_type` set event_type_name='".$data['event_type_name']."',category_name='".$data['category_name']."',updated_date='".date("Y-m-d h:i:s")."' where event_type_id='".$data['event_type_id']."'";
	//print_r($query);exit;
	$result=$this->conn->dbCommand($query);		
	if($result){
			return array('message'=>'Update Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	}
	public function get_event_type($data){
		$query = "select * from event_type where event_type_id='".$data['event_type_id']."'";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Updated Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function delete_event_type($data)
	{	
		$query = "update event_type set status='".$data['status']."' where event_type_id='".$data['event_type_id']."'";
		$result=$this->conn->dbCommand($query);
		if($result){
			return array('message'=>"Successfully deleted");
		}
		else
		return array('message'=>'Deletion Failed');
		
	}
	public function all_event_types($data)
	{
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['results_per_page']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="select * from `event_type`";
	$query_string1="SELECT count(*) as total from event_type";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	
		if($data['search_by']=='status')
		{	
			$search_by = "status";
			
		}else if($data['search_by'] == 'event_type_name')
		{
			
			$search_by = "event_type_name";
			
		}else if($data['search_by'] == 'category_name')
		{
			$search_by = "category_name";
		}
		$query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		 
	}else
	{
	$query = $query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = $query_string1;
	}	
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All event types","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No event types');
	}
	public function get_event_type_list()
	{
		$query = "select event_type_id,event_type_name,category_name from event_type";
		$result = $this->conn->dbMultiple($query);
		if($result)
		{
			return array('message'=>'all event types Successfully', 'result'=>$result);
		}else
		{
			return array('message'=>'all event type get Failed');
		}
	}
	public function create_event_location($data)
	{
		$query = "insert into event_location (event_type_id,location_name,location_address,location_city,created_date,updated_date) values ('".$data['event_type_id']."','".$data['location_name']."','".$data['location_address']."','".$data['location_city']."','".date("Y-m-d h:i:s")."','".date("Y-m_d h:i:s")."')";
			$result = $this->conn->dbCommand($query);
			if($result)
			{
				return array('message'=> 'Successfully Created');
			}else 
			{
				return array('message'=>'Creation Failed');
			}
		
	}
	public function edit_event_location($data)
	{	
	$query="update `event_location` set event_type_id='".$data['event_type_id']."',location_name='".$data['location_name']."',location_address='".$data['location_address']."',location_city='".$data['location_city']."',updated_date='".date("Y-m-d h:i:s")."' where location_id='".$data['location_id']."'";
	//print_r($query);exit;
	$result=$this->conn->dbCommand($query);		
	if($result){
			return array('message'=>'Update Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	}
	public function get_event_location($data){
		$query = "select el.*, et.event_type_id,et.event_type_name from event_location el left join event_type et on el.event_type_id=et.event_type_id where location_id='".$data['location_id']."'";
		$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Updated Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function delete_event_location($data)
	{	
		$query = "update event_location set status='".$data['status']."' where location_id='".$data['location_id']."'";
		$result=$this->conn->dbCommand($query);
		if($result){
			return array('message'=>"Successfully deleted");
		}
		else
		return array('message'=>'Deletion Failed');
		
	}
	public function all_event_location($data)
	{
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['results_per_page']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="select * from `event_location`";
	$query_string1="SELECT count(*) as total from event_location";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	
		if($data['search_by']=='status')
		{	
			$search_by = "status";
		}
		else if($data['search_by'] == 'location_name')
		{
			$search_by = "location_name";
		}
		else if($data['search_by'] == 'location_address')
		{
			$search_by = "location_address";
		}
		else if($data['search_by'] == 'location_city')
		{
			
			$search_by = "location_city";
			
		}
		$query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		 
	}else
	{
	$query = $query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = $query_string1;
	}	
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All event locations","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No event locations');
	}
	public function get_event_location_list()
	{
		$query = "select location_id,location_name from event_location";
		$result = $this->conn->dbMultiple($query);
		if($result)
		{
			return array('message'=>'all event types Successfully', 'result'=>$result);
		}else
		{
			return array('message'=>'all event type get Failed');
		}
	}
	public function create_event_price($data)
	{
		if($query = "insert into event_pricing (event_type_id,pricing_name,start_date,end_date,created_date,updated_date) values ('".$data['event_type_id']."','".$data['pricing_name']."','".$data['start_date']."','".$data['end_date']."','".date("Y-m-d h:i:s")."','".date("Y-m_d h:i:s")."')")
		{
			$result = $this->conn->dbCommand($query);
			$lastId=$this->conn->dbLastId();
			$pricing_fors = explode(',',$data['pricing_for']);
			$rates = explode(',',$data['rate']);
			//print_r($rates);exit;
		foreach($pricing_fors as $id=>$pricing_for)
		{	
			$query1 = "insert into pricing (event_pricing_id,pricing_for,rate,created_date,updated_date) values('".$lastId."','".$pricing_fors[$id]."','".$rates[$id]."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
			$result=$this->conn->dbCommand($query1);
		}
			
			
			//print_r($query1);exit;
			//$result = $this->conn->dbCommand($query1);
			if($result)
			{
				return array('message'=> 'Successfully Created');
			}else 
			{
				return array('message'=>'Creation Failed');
			}
		}
	}
	public function edit_event_price($data)
	{	
	if($query="update `event_pricing` set event_type_id='".$data['event_type_id']."',pricing_name='".$data['pricing_name']."',start_date='".$data['start_date']."',end_date='".$data['end_date']."',updated_date='".date("Y-m-d h:i:s")."' where event_pricing_id='".$data['event_pricing_id']."'")
	{
	//print_r($query);exit;
	$result=$this->conn->dbCommand($query);		
	$query1 = "delete from pricing where event_pricing_id=".$data['event_pricing_id'];
	$result=$this->conn->dbCommand($query1);		
	$pricing_fors = explode(',',$data['pricing_for']);
			$rates = explode(',',$data['rate']);
			//print_r($rates);exit;
		foreach($pricing_fors as $id=>$pricing_for)
		{	
			$query1 = "insert into pricing (event_pricing_id,pricing_for,rate,created_date,updated_date) values('".$data['event_pricing_id']."','".$pricing_fors[$id]."','".$rates[$id]."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
			$result=$this->conn->dbCommand($query1);
		}
	if($result){
			return array('message'=>'Update Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	}
	}
	public function get_event_price($data)
	{
		$query = "select * from event_pricing where event_pricing_id='".$data['event_pricing_id']."'";
		
			$result=$this->conn->dbMultiple($query);
				
		if($result){
			return array('message'=>'Get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Get Failed');
		}	
	     
	}
	public function get_event_price_list($data)
	{
	$query = "SELECT event_pricing_id,pricing_name FROM event_pricing";
	//print_r($query);exit;
			$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Get Failed');
		}	
	}
	public function get_event_pricing($data)
	{
	$query = "SELECT pricing_id,pricing_for,rate FROM pricing WHERE event_pricing_id='".$data['event_pricing_id']."'";
	//print_r($query);exit;
			$result=$this->conn->dbMultiple($query);		
		if($result){
			return array('message'=>'Get Successfully', 'result'=>$result);			
		}else{
			return array('message'=>'Get Failed');
		}	
	}
	public function delete_event_price($data)
	{	
		if($query = "update event_pricing set status='".$data['status']."' where event_pricing_id='".$data['event_pricing_id']."'")
		{
		$result=$this->conn->dbCommand($query);
		$query1 = "update pricing set status='".$data['status']."' where event_pricing_id='".$data['event_pricing_id']."'";
		$result=$this->conn->dbCommand($query1);
		if($result){
			return array('message'=>"Successfully deleted");
		}
		else
		return array('message'=>'Deletion Failed');
		}
	}
	public function all_event_price($data)
	{
	$current_page=0;
	$results_per_page=10;
	$order_by="ep.created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['results_per_page']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="select ep.*,et.event_type_id,et.event_type_name from `event_pricing` ep left join event_type et on ep.event_type_id=et.event_type_id";
	$query_string1="SELECT count(*) as total from `event_pricing` ep left join event_type et on ep.event_type_id=et.event_type_id";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	
		if($data['search_by']=='status')
		{	
			$search_by = "status";
		}
		else if($data['search_by'] == 'pricing_name')
		{
			$search_by = "pricing_name";
		}
		else if($data['search_by'] == 'event_type_name')
		{
			$search_by = "et.event_type_name";
		}
		else if($data['search_by'] == 'start_date')
		{
			
			$search_by = "start_date";
			
		}
		$query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		 
	}else
	{
	$query = $query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = $query_string1;
	}	
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All event prices","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No event Prices');
	}
	public function create_event_offer($data)
	 {	//print_r($data);exit;
		if($data['minimum_tickets']!="" || $data['discount_type']!="")
		{	
			if($data['minimum_tickets']!="" && $data['discount_type']!="")
			{
				return array('message'=>'Please select any one of offer on Ticket or Discount on price');
			}else
			{
				if($data['minimum_tickets']!="")
				{
					$query = "insert into event_offers (offer_code,start_date,end_date,description,minimum_tickets,free_tickets,created_date,updated_date) values('".$data['offer_code']."','".$data['start_date']."','".$data['end_date']."','".$data['description']."','".$data['minimum_tickets']."','".$data['free_tickets']."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
				}else if($data['discount_type']!="")
				{
					$query = "insert into event_offers (offer_code,start_date,end_date,description,discount_type,minimum_amount,disc_amount,created_date,updated_date) values('".$data['offer_code']."','".$data['start_date']."','".$data['end_date']."','".$data['description']."','".$data['discount_type']."','".$data['minimum_amount']."','".$data['disc_amount']."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
				} 
				
			
				$result = $this->conn->dbCommand($query);
				//print_r($rates);exit;
				if($result)
				{
					return array('message'=> 'Successfully Created');
				}else 
				{
					return array('message'=>'Creation Failed');
				}
			}
		}else 
		{
			return array('message'=>'Please select any one of offer on Ticket or Discount on price');
		}
	}
	public function edit_event_offer($data)
	 {	//print_r($data);exit;
		if($data['minimum_tickets']!="" || $data['discount_type']!="")
		{	
			if($data['minimum_tickets']!="" && $data['discount_type']!="")
			{
				return array('message'=>'Please select any one of offer on Ticket or Discount on price');
			}else
			{
				if($data['minimum_tickets']!="")
				{
					$query = "update event_offers set offer_code='".$data['offer_code']."',start_date='".$data['start_date']."',end_date='".$data['end_date']."',description='".$data['description']."',minimum_tickets='".$data['minimum_tickets']."',free_tickets='".$data['free_tickets']."',discount_type='0',minimum_amount='0',disc_amount='0',status='".$data['status']."',updated_date='".date("Y-m-d h:i:s")."' where offer_id='".$data['offer_id']."'";
				}else if($data['discount_type']!="")
				{
					$query = "update event_offers set offer_code='".$data['offer_code']."',start_date='".$data['start_date']."',end_date='".$data['end_date']."',description='".$data['description']."',minimum_tickets='0',free_tickets='0',discount_type='".$data['discount_type']."',minimum_amount='".$data['minimum_amount']."',disc_amount='".$data['disc_amount']."',status='".$data['status']."',updated_date='".date("Y-m-d h:i:s")."' where offer_id='".$data['offer_id']."'";
				} 
				
				//print_r($query);exit;
				$result = $this->conn->dbCommand($query);
				if($result)
				{
					return array('message'=> 'updated Created');
				}else 
				{
					return array('message'=>'updates Failed');
				}
			}
		}else 
		{
			return array('message'=>'Please select any one of offer on Ticket or Discount on price');
		}
	}
	public function get_event_offer($data)
	{
		$query = "select * from event_offers where offer_id=".$data['offer_id'];
		$result = $this->conn->dbMultiple($query);
		if($result)
		{
			return array('message'=>'offer get Successfully', 'result'=>$result);
		}else 
		{
			return array('message'=>'offer get failed');
		}
	}
	public function delete_event_offer($data)
	{
		$query = "update event_offers set status='".$data['status']."' where offer_id=".$data['offer_id'];
		$result = $this->conn->dbCommand($query);
		if($result)
		{
			return array('message'=>'offer deleted Successfully');
		}else 
		{
			return array('message'=>'offer deletion failed');
		}
	}
	public function all_event_offer($data)
	{
	$current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['results_per_page']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="select * from `event_offers`";
	$query_string1="SELECT count(*) as total from `event_offers`";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	
		if($data['search_by']=='offer_code')
		{	
			$search_by = "offer_code";
		}
		else if($data['search_by'] == 'end_date')
		{
			$search_by = "end_date";
		}
		else if($data['search_by'] == 'disc_amount')
		{
			$search_by = "disc_amount";
		}
		else if($data['search_by'] == 'start_date')
		{
			
			$search_by = "start_date";
			
		}
		$query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		 
	}else
	{
	$query = $query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = $query_string1;
	}	
		//print_r($query);exit;
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All offers","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No offers');
	}
	public function get_offer_list()
	{
		$query = "select offer_id,offer_code from event_offers";
		$result = $this->conn->dbMultiple($query);
		if($result)
		{
			return array('message'=>'all event types Successfully', 'result'=>$result);
		}else
		{
			return array('message'=>'all event type get Failed');
		}
	}
	public function create_event($data)
	 {	
		$query = "select category_name from event_type where event_type_id='".$data['event_type_id']."'";
		$result = $this->conn->dbMultiple($query);
		
 		//print_r($result);
		//echo "<pre>"; print_r($data);exit;
		
		if($result[0]['category_name']=="event")
		{	
			
				
				$file = $data['event_image']['tmp_name'];
				$filename = time().$data["event_image"]['name'];
				$event_image_path="uploads/events/".$filename;
				move_uploaded_file($file, $event_image_path);
				
				$file1 = $data['sponsor_logo']['tmp_name'];
				$filename1 = time().$data["sponsor_logo"]['name'];
				$sponsor_logo_path="uploads/events/".$filename1;
				move_uploaded_file($file1, $sponsor_logo_path);
				
				$file2 = $data['sponsor_email_logo']['tmp_name'];
				$filename2 = time().$data["sponsor_email_logo"]['name'];
				$sponsor_email_logo_path="uploads/events/".$filename2;
				move_uploaded_file($file2, $sponsor_email_logo_path);
				
				$file3 = $data['flyer_logo']['tmp_name'];
				$filename3 = time().$data["flyer_logo"]['name'];
				$flyer_logo_path="uploads/events/".$filename3;
				move_uploaded_file($file3, $flyer_logo_path);
				//print_r($data);exit;
				
				if($query = "insert into events (event_title,event_type_id,association_id,credit_card_charges,event_image,event_image_title,event_description,sponsor_logo,sponsor_title,sponsor_email_logo,sponsor_email_title,flyer_logo,flyer_title,sponsor_url,donate,is_upcomming_event,created_date,updated_date) values('".$data['event_title']."','".$data['event_type_id']."','".$data['association_id']."','".$data['credit_card_charges']."','".$this->conn->baseurl().$event_image_path."','".$data['event_image']['name']."','".$data['event_description']."','".$this->conn->baseurl().$sponsor_logo_path."','".$data['sponsor_logo']['name']."','".$this->conn->baseurl().$sponsor_email_logo_path."','".$data['sponsor_email_logo']['name']."','".$this->conn->baseurl().$flyer_logo_path."','".$data['flyer_logo']['name']."','".$data['sponsor_url']."','".$data['donate']."','".$data['is_upcomming_event']."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')")
				{
					//print_r($query);exit;
				$result = $this->conn->dbCommand($query);
				$lastId = $this->conn->dbLastId();
				//print_r($lastId);exit;
				
				$location_ids = explode(',',$data['location_id']);
				$event_pricing_ids = explode(',',$data['event_pricing_id']);
				$event_dates = explode(',',$data['event_date']);
				$event_times = explode(',',$data['event_time']);
				$number_of_ticket = explode(',',$data['number_of_tickets']);
				$stop_tickets = explode(',',$data['stop_ticket']);
				$event_location_time_ids = explode(',',$data['event_location_time_id']);
				foreach($location_ids as $id=>$location_id){
			$query1 = "insert into event_location_times (event_id,location_id,event_date,event_time,number_of_tickets,stop_ticket,event_pricing_id,created_date,updated_date) values('".$lastId."','".$location_ids[$id]."','".$event_dates[$id]."','".$event_times[$id]."','".$number_of_ticket[$id]."','".$stop_tickets[$id]."','".$event_pricing_ids[$id]."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
			
				$result1 = $this->conn->dbCommand($query1);
				} 
				if($result1)
				{
					return array('message'=> 'Successfully Created');
				}else 
				{
					return array('message'=>'Creation Failed');
				}	
				}
			
		}
		 else if($result[0]['category_name']=="movies")
		{	
			$file = $data['event_image']['tmp_name'];
				$filename = time().$data["event_image"]['name'];
				$event_image_path="uploads/events/".$filename;
				move_uploaded_file($file, $event_image_path);
				
				$file1 = $data['sponsor_logo']['tmp_name'];
				$filename1 = time().$data["sponsor_logo"]['name'];
				$sponsor_logo_path="uploads/events/".$filename1;
				move_uploaded_file($file1, $sponsor_logo_path);
				
				$file2 = $data['sponsor_email_logo']['tmp_name'];
				$filename2 = time().$data["sponsor_email_logo"]['name'];
				$sponsor_email_logo_path="uploads/events/".$filename2;
				move_uploaded_file($file2, $sponsor_email_logo_path);
				
				$file3 = $data['flyer_logo']['tmp_name'];
				$filename3 = time().$data["flyer_logo"]['name'];
				$flyer_logo_path="uploads/events/".$filename3;
				move_uploaded_file($file3, $flyer_logo_path);
				print_r($data);exit;
			if($query = "insert into events (event_title,event_type_id,association_id,credit_card_charges,event_image,event_image_title,event_description,sponsor_logo,sponsor_title,sponsor_email_logo,sponsor_email_title,flyer_logo,flyer_title,sponsor_url,donate,is_upcomming_event,created_date,updated_date) values('".$data['event_title']."','".$data['event_type_id']."','".$data['association_id']."','".$data['credit_card_charges']."','".$this->conn->baseurl().$event_image_path."','".$data['event_image']['name']."','".$data['event_description']."','".$this->conn->baseurl().$sponsor_logo_path."','".$data['sponsor_logo']['name']."','".$this->conn->baseurl().$sponsor_email_logo_path."','".$data['sponsor_email_logo']['name']."','".$this->conn->baseurl().$flyer_logo_path."','".$data['flyer_logo']['name']."','".$data['sponsor_url']."','".$data['donate']."','".$data['is_upcomming_event']."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')")
			{		
				 $result = $this->conn->dbCommand($query);
				$lastId = $this->conn->dbLastId();
				$query1 = "insert into event_movie_details (event_id,director,producer,music_director,release_date,star_cast,created_date,updated_date) values ('".$lastId."','".$data['director']."','".$data['producer']."','".$data['music_director']."','".$data['release_date']."','".$data['star_cast']."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
				
					$result = $this->conn->dbCommand($query1);
					foreach($data["movie_image"]['tmp_name'] as $id=>$filetmpname){
			
					$filename=time().$data["movie_image"]['name'][$id];
					//echo "<pre>";print_r($filename);
					$target_file="uploads/events/".$filename;
					move_uploaded_file($filetmpname, $target_file);
					
					$query = "insert into event_movie_images (event_id,movie_image,movie_image_title,created_date,updated_date) values('".$lastId."','".$this->conn->baseurl().$target_file."','".$data["movie_image"]['name'][$id]."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
					
						$result = $this->conn->dbCommand($query);
					}	
					foreach($data["starcast_image"]['tmp_name'] as $id=>$starcast_image){
					
					$filename=time().$data["starcast_image"]['name'][$id];
					//echo "<pre>";print_r($filename);
					$target_file="uploads/events/".$filename;
					move_uploaded_file($starcast_image, $target_file);
					$query1 = "insert into event_movie_starcast_images (event_id,starcast_name,starcast_image_name,starcast_image_title,created_date,updated_date) values('".$lastId."','".$data["starcast_image"]['name'][$id]."','".$this->conn->baseurl().$target_file."','".$data["starcast_image"]['title'][$id]."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
						//print_r($query1);exit;
						$result = $this->conn->dbCommand($query1);
					}
					$video_names = explode(',',$data['video_name']);
					$video_urls = explode(',',$data['video_url']);
					$video_image_urls = explode(',',$data['video_image_url']);
					//print_r($video_image_urls);exit;
					foreach($video_names as $id=>$video_name){
					$filename=time().$data["star_cast"]['name'][$id];
					//echo "<pre>";print_r($filename);
					$target_file="uploads/events/".$filename;
					move_uploaded_file($filetmpname, $target_file);
					$query1 = "insert into event_movie_youtube_videos (event_id,video_name,video_url,video_image_url,created_date,updated_date) values('".$lastId."','".$video_names[$id]."','".$video_urls[$id]."','".$video_image_urls[$id]."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
						$result = $this->conn->dbCommand($query1);
					}
				$location_ids = explode(',',$data['location_id']);
				$event_pricing_ids = explode(',',$data['event_pricing_id']);
				$event_dates = explode(',',$data['event_date']);
				$event_times = explode(',',$data['event_time']);
				$number_of_ticket = explode(',',$data['number_of_tickets']);
				$stop_tickets = explode(',',$data['stop_ticket']);
				$event_location_time_ids = explode(',',$data['event_location_time_id']);
				foreach($location_ids as $id=>$location_id){
			$query = "insert into event_location_times (event_id,location_id,event_date,event_time,number_of_tickets,stop_ticket,event_pricing_id,created_date,updated_date) values('".$lastId."','".$location_ids[$id]."','".$event_dates[$id]."','".$event_times[$id]."','".$number_of_ticket[$id]."','".$stop_tickets[$id]."','".$event_pricing_ids[$id]."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
				$result = $this->conn->dbCommand($query);
				}
				if($result)
				{
					return array('message'=> 'Successfully Created');
				}else 
				{
					return array('message'=>'Creation Failed');
				}
				
			}	
		} 
		
	}
	public function get_event($data)
	 {	
		$query = "select et.category_name from events e left join event_type et on e.event_type_id=et.event_type_id where event_id='".$data['event_id']."'";
		//print_r($query);exit;
		$result = $this->conn->dbMultiple($query);
		
 		//print_r($result);
		//echo "<pre>"; print_r($result[0]);exit;
		
		if($result[0]['category_name']=="event")
		{
			$query1 = "select e.*,a.association_name from events e left join event_location_times elt on e.event_id=elt.event_id left join event_pricing ep on elt.event_pricing_id=ep.event_pricing_id left join event_location el on elt.location_id=el.location_id left join association a on e.association_id=a.association_id where e.event_id='".$data['event_id']."'";
			$result = $this->conn->dbMultiple($query1);
			$query = "select p.pricing_for,p.rate from events e left join event_location_times elt on e.event_id=elt.event_id left join event_pricing ep on elt.event_pricing_id=ep.event_pricing_id left join pricing p on ep.event_pricing_id=p.event_pricing_id where e.event_id='".$data['event_id']."'";
			$result1 = $this->conn->dbMultiple($query);
			$query2 = "select el.location_name,elt.*,ep.pricing_name from events e left join event_location_times elt on e.event_id=elt.event_id left join event_pricing ep on elt.event_pricing_id=ep.event_pricing_id left join event_location el on elt.location_id=el.location_id left join association a on e.association_id=a.association_id where e.event_id='".$data['event_id']."'";
			//print_r($query2);exit;
			$result2 = $this->conn->dbMultiple($query2);
			if($result2)
			{
				return array('message'=>'event get by id Successfully', 'event'=>$result,'locations'=>$result2,$result1);
			}else
			{
				return array('message'=>'event get by id Failed');
			}
		}else if($result[0]['category_name']=="movies")
		{		
			$query1 = "select e.*,a.association_name from events e left join association a on e.association_id=a.association_id where e.event_id='".$data['event_id']."'";
			$result = $this->conn->dbMultiple($query1);
			$query = "select p.pricing_for,p.rate from events e left join event_location_times elt on e.event_id=elt.event_id left join event_pricing ep on elt.event_pricing_id=ep.event_pricing_id left join pricing p on ep.event_pricing_id=p.event_pricing_id where e.event_id='".$data['event_id']."'";
			$result1 = $this->conn->dbMultiple($query);
			$query2 = "select el.location_name,elt.*,ep.pricing_name from events e left join event_location_times elt on e.event_id=elt.event_id left join event_pricing ep on elt.event_pricing_id=ep.event_pricing_id left join event_location el on elt.location_id=el.location_id left join association a on e.association_id=a.association_id where e.event_id='".$data['event_id']."'";
			//print_r($query2);exit;
			$result2 = $this->conn->dbMultiple($query2);
	
			
			$query3 = "select emd.movie_detail_id,emd.director,emd.producer,emd.music_director,emd.release_date,emd.star_cast from events e left join event_movie_details emd on e.event_id=emd.event_id where e.event_id='".$data['event_id']."'";
			$result3 = $this->conn->dbMultiple($query3);
			$query4 = "select emi.movie_image_id,emi.movie_image,emi.movie_image_title from events e left join event_movie_images emi on e.event_id=emi.event_id where e.event_id='".$data['event_id']."'";
			$result4 = $this->conn->dbMultiple($query4);
			$query5 = "select sci.movie_starcast_image_id,sci.starcast_name,sci.starcast_image_name,sci.starcast_image_title from events e left join event_movie_starcast_images sci on e.event_id=sci.event_id where e.event_id='".$data['event_id']."'";
			//print_r($query3);exit;
			$result5 = $this->conn->dbMultiple($query5);
			$query6 = "select eyv.movie_youtube_video_id,eyv.video_name,eyv.video_url,eyv.video_image_url from events e left join event_movie_youtube_videos eyv on e.event_id=eyv.event_id where e.event_id='".$data['event_id']."'";
			//print_r($query4);exit;
			$result6 = $this->conn->dbMultiple($query6);
			if($result6)
			{
				return array('message'=>'event get by id Successfully', 'event'=>$result ,'locations'=>array($result2,$result1) , 'moviedetail'=>array($result3,$result4,$result5,$result6));
			}else
			{
				return array('message'=>'event get by id Failed');
			}
		}
	
	 }
	 
	public function edit_event($data)
	 {	
		$query = "select category_name from event_type where event_type_id='".$data['event_type_id']."'";
		$result = $this->conn->dbMultiple($query);
		
 		//print_r($result[0]);
		//echo "<pre>"; print_r($data);exit;
		
		if($result[0]['category_name']=="event")
		{	
			
				if($data['event_image']['tmp_name']!=""){
				$file = $data['event_image']['tmp_name'];
				$filename = time().$data["event_image"]['name'];
				$event_image_path="uploads/events/".$filename;
				move_uploaded_file($file, $event_image_path);
				}else
				{
					$event_image_path = "";
					$data["event_image"]['name'] = "";
				}
				if($data['sponsor_logo']['tmp_name']!=""){
				$file1 = $data['sponsor_logo']['tmp_name'];
				$filename1 = time().$data["sponsor_logo"]['name'];
				$sponsor_logo_path="uploads/events/".$filename1;
				move_uploaded_file($file1, $sponsor_logo_path);
				}else
				{
					$sponsor_logo_path = "";
					$data["sponsor_logo"]['name'] = "";
				}
				if($data['sponsor_email_logo']['tmp_name']!=""){
				$file2 = $data['sponsor_email_logo']['tmp_name'];
				$filename2 = time().$data["sponsor_email_logo"]['name'];
				$sponsor_email_logo_path="uploads/events/".$filename2;
				move_uploaded_file($file2, $sponsor_email_logo_path);
				}else
				{
					$sponsor_email_logo_path = "";
					$data["sponsor_email_logo"]['name'] = "";
					
				}
				if($data['flyer_logo']['tmp_name']!=""){
				$file3 = $data['flyer_logo']['tmp_name'];
				$filename3 = time().$data["flyer_logo"]['name'];
				$flyer_logo_path="uploads/events/".$filename3;
				move_uploaded_file($file3, $flyer_logo_path);
				}else
				{
					$flyer_logo_path = "";
					$data["flyer_logo"]['name'] = "";
				}
				
				
				//print_r($data);exit;
	    $query="update `events` set event_title='".$data['event_title']."',event_type_id='".$data['event_type_id']."',association_id='".$data['association_id']."',credit_card_charges='".$data['credit_card_charges']."'".($event_image_path!="" ? ",event_image='".$this->conn->baseurl().$event_image_path : '')."".($data["event_image"]['name']!="" ? ",event_image_title='".$data["event_image"]['name'] : '').",event_description='".$data['event_description']."'".($sponsor_logo_path!="" ? ',event_image='.$this->conn->baseurl().$sponsor_logo_path : '')."".($data["sponsor_logo"]['name']!="" ? ',event_image_title='.$data["sponsor_logo"]['name'] : '')."".($sponsor_email_logo_path!="" ? ',event_image='.$this->conn->baseurl().$sponsor_email_logo_path : '')."".($data["sponsor_email_logo"]['name']!="" ? ',sponsor_email_title='.$data["sponsor_email_logo"]['name'] : '')."".($flyer_logo_path!="" ? ',event_image='.$this->conn->baseurl().$flyer_logo_path : '')."".($data["flyer_logo"]['name']!="" ? ',flyer_title='.$data["flyer_logo"]['name'] : '')." where event_id='".$data['event_id']."'";
		//print_r($query);exit;
		$result = $this->conn->dbCommand($query);
		
				
				/* $location_ids = explode(',',$data['location_id']);
				$event_pricing_ids = explode(',',$data['event_pricing_id']);
				$event_dates = explode(',',$data['event_date']);
				$event_times = explode(',',$data['event_time']);
				$number_of_ticket = explode(',',$data['number_of_tickets']);
				$stop_tickets = explode(',',$data['stop_ticket']);
				$event_location_time_ids = explode(',',$data['event_location_time_id']); */
				//$event_locations = json_decode($data['event_locations']);
				//print_r($event_locations);exit;
				if($data['remove_location']!="")
				{		
					$remove_locations = explode(',',$data['remove_location']);
				foreach($remove_locations as $id=>$movie_remove_id)
				{
				$query = "delete from event_movie_images where movie_image_id='".$movie_remove_id."'";
					//print_r($query1);exit;
				$result = $this->conn->dbCommand($query);
			    }
				}
			foreach($event_locations as $id=>$location_id)
			{	
				if(isset($event_locations[$id]->event_location_time_id) && $event_locations[$id]->event_location_time_id!="")
				{
					
			$query1 = "update event_location_times set event_id='".$data['event_id']."',location_id='".$event_locations[$id]->location_id."',event_date='".$event_locations[$id]->event_date."',event_time='".$event_locations[$id]->event_time."',number_of_tickets='".$event_locations[$id]->number_of_tickets."',stop_ticket='".$event_locations[$id]->stop_ticket."',event_pricing_id='".$event_locations[$id]->event_pricing_id."',updated_date='".date("Y-m-d h:i:s")."' where event_location_time_id='".$event_locations[$id]->event_location_time_id."'";
			//print_r($query1);exit;
				$result1 = $this->conn->dbCommand($query1);
			}
			else
			{
				$query2 = "insert into event_location_times (event_id,location_id,event_date,event_time,number_of_tickets,stop_ticket,event_pricing_id,created_date,updated_date) values ('".$data['event_id']."','".$event_locations[$id]->location_id."','".$event_locations[$id]->event_date."','".$event_locations[$id]->event_time."','".$event_locations[$id]->number_of_tickets."','".$event_locations[$id]->stop_ticket."','".$event_locations[$id]->event_pricing_id."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
				//print_r($query2);exit;
				$result2 = $this->conn->dbCommand($query2);
			}	
			}
				if($result2)
				{
					return array('message'=> 'Successfully Updated');
				}else 
				{
					return array('message'=>'Updation Failed');
				} 
				
			
	  
	 }
	 else if($result[0]['category_name']=="movies")
	{		
			//print_r($data);exit;
				if($data['event_image']!=""){
				$file = $data['event_image']['tmp_name'];
				$filename = time().$data["event_image"]['name'];
				$event_image_path="uploads/events/".$filename;
				move_uploaded_file($file, $event_image_path);
				}else
				{
					$event_image_path = "";
					$data["event_image"]['name'] = "";
				}
				if($data['sponsor_logo']!=""){
				$file1 = $data['sponsor_logo']['tmp_name'];
				$filename1 = time().$data["sponsor_logo"]['name'];
				$sponsor_logo_path="uploads/events/".$filename1;
				move_uploaded_file($file1, $sponsor_logo_path);
				}else
				{
					$sponsor_logo_path = "";
					$data["sponsor_logo"]['name'] = "";
				}
				if($data['sponsor_email_logo']!=""){
				$file2 = $data['sponsor_email_logo']['tmp_name'];
				$filename2 = time().$data["sponsor_email_logo"]['name'];
				$sponsor_email_logo_path="uploads/events/".$filename2;
				move_uploaded_file($file2, $sponsor_email_logo_path);
				}else
				{
					$sponsor_email_logo_path = "";
					$data["sponsor_email_logo"]['name'] = "";
					
				}
				if($data['flyer_logo']!=""){
				$file3 = $data['flyer_logo']['tmp_name'];
				$filename3 = time().$data["flyer_logo"]['name'];
				$flyer_logo_path="uploads/events/".$filename3;
				move_uploaded_file($file3, $flyer_logo_path);
				}else
				{
					$flyer_logo_path = "";
					$data["flyer_logo"]['name'] = "";
				}
				
				
				//print_r($data);exit;
	    $query="update `events` set event_title='".$data['event_title']."',event_type_id='".$data['event_type_id']."',association_id='".$data['association_id']."',credit_card_charges='".$data['credit_card_charges']."'".($event_image_path!="" ? ",event_image='".$this->conn->baseurl().$event_image_path : '')."'".($data["event_image"]['name']!="" ? ",event_image_title='".$data["event_image"]['name'] : '')."',event_description='".$data['event_description']."'".($sponsor_logo_path!="" ? ",event_image='".$this->conn->baseurl().$sponsor_logo_path : '')."'".($data["sponsor_logo"]['name']!="" ? ",event_image_title='".$data["sponsor_logo"]['name'] : '')."'".($sponsor_email_logo_path!="" ? ",event_image='".$this->conn->baseurl().$sponsor_email_logo_path : '')."'".($data["sponsor_email_logo"]['name']!="" ? ",sponsor_email_title='".$data["sponsor_email_logo"]['name'] : '')."'".($flyer_logo_path!="" ? ",event_image='".$this->conn->baseurl().$flyer_logo_path : '')."'".($data["flyer_logo"]['name']!="" ? ",flyer_title='".$data["flyer_logo"]['name'] : '')."' where event_id='".$data['event_id']."'";
		//print_r($query);exit;
		$result = $this->conn->dbCommand($query);
				
				if($data['director']!="")
				{
					$query = "update event_movie_details set event_id='".$data['event_id']."',director='".$data['director']."',producer='".$data['producer']."',music_director='".$data['music_director']."',release_date='".$data['release_date']."',star_cast='".$data['star_cast']."',updated_date='".date("Y-m-d h:i:s")."' where movie_detail_id='".movie_detail_id."' ";
					$result = $this->conn->dbCommand($query);
					
				}
				//$event_locations = json_decode($data['event_locations']);
				//print_r($event_locations);exit;
				if($data['remove_location']!="")
				{		
					$remove_locations = explode(',',$data['remove_location']);
				foreach($remove_locations as $id=>$movie_remove_id)
				{
				$query = "delete from event_movie_images where movie_image_id='".$movie_remove_id."'";
					//print_r($query1);exit;
				$result = $this->conn->dbCommand($query);
			    }
				}
			foreach($event_locations as $id=>$location_id)
			{	
				if(isset($event_locations[$id]->event_location_time_id) && $event_locations[$id]->event_location_time_id!="")
				{
					
			$query1 = "update event_location_times set event_id='".$data['event_id']."',location_id='".$event_locations[$id]->location_id."',event_date='".$event_locations[$id]->event_date."',event_time='".$event_locations[$id]->event_time."',number_of_tickets='".$event_locations[$id]->number_of_tickets."',stop_ticket='".$event_locations[$id]->stop_ticket."',event_pricing_id='".$event_locations[$id]->event_pricing_id."',updated_date='".date("Y-m-d h:i:s")."' where event_location_time_id='".$event_locations[$id]->event_location_time_id."'";
			//print_r($query1);exit;
				$result1 = $this->conn->dbCommand($query1);
			}
			else
			{
				$query2 = "insert into event_location_times (event_id,location_id,event_date,event_time,number_of_tickets,stop_ticket,event_pricing_id,created_date,updated_date) values ('".$data['event_id']."','".$event_locations[$id]->location_id."','".$event_locations[$id]->event_date."','".$event_locations[$id]->event_time."','".$event_locations[$id]->number_of_tickets."','".$event_locations[$id]->stop_ticket."','".$event_locations[$id]->event_pricing_id."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
				$result2 = $this->conn->dbCommand($query2);
			}	
			}
				
					
				if($data['movie_remove_id']!="")
				{	
				foreach($data['movie_remove_id'] as $id=>$movie_remove_id)
				{
				$query = "delete from event_movie_images where movie_image_id='".$movie_remove_id."'";
					//print_r($query1);exit;
				$result = $this->conn->dbCommand($query);
			    }
				}
				if($data['movie_image_id']!="" && $data["movie_image"]['tmp_name']!="")
				{
					$movie_image_ids = explode(',',$data['movie_image_id']);
					$movie_names = explode(',',$data['movie_name']);
				
				foreach($data["movie_image"]['name'] as $id=>$filename)
				{
				if(in_array($filename, $movie_names))
				{
				$file = $data["movie_image"]['tmp_name'][$id];
				$filename=time().$filename;
				$movie_image_path="uploads/events/".$filename;
				move_uploaded_file($filetmpname, $movie_image_path);
				$query3 = "update event_movie_images set event_id='".$data['event_id']."',movie_image='".$this->conn->baseurl().$movie_image_path."',movie_image_title='".$filename."',updated_date='".date("Y-m-d h:i:s")."' where movie_image_id='".$movie_image_ids[$id]."'";
				//print_r($query);exit;
				$result = $this->conn->dbCommand($query3);
				}
				else
				{
				$file = $data["movie_image"]['tmp_name'][$id];
				$filename=time().$filename;
				$movie_image_path="uploads/events/".$filename;
				move_uploaded_file($filetmpname, $movie_image_path);
				$query4 = "insert into event_movie_images (event_id,movie_image,movie_image_title,created_date,updated_date) values('".$data['event_id']."','".$this->conn->baseurl().$movie_image_path."','".$data["movie_image"]['name'][$id]."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
				//print_r($query);exit;
				$result = $this->conn->dbCommand($query4);
				}	
				}
				}
				if($data['starcast_remove_id']!="")
				{	//echo "<pre>";print_r($data['image_id']);exit;
				foreach($data['starcast_remove_id'] as $id=>$starcast_remove_id)
				{
				$query = "delete from event_movie_starcast_images where movie_starcast_image_id='".$starcast_remove_id."'";
					//print_r($query1);exit;
				$result = $this->conn->dbCommand($query);
			    }
				}
				
				if($data['movie_starcast_image_id']!="" && $data["starcast_image"]['tmp_name']!="")
				{
					$movie_starcast_image_ids = explode(',',$data['movie_starcast_image_id']);
					$starcast_image_names = explode(',',$data['starcast_image_name']);
					
					foreach($data["starcast_image"]['name'] as $id=>$filename)
				{
				if(in_array($filename, $starcast_image_names))
				{
				$file = $data["starcast_image"]['tmp_name'][$id];
				$filename=time().$filename;
				$starcast_image_path="uploads/events/".$filename;
				move_uploaded_file($filetmpname, $movie_image_path);
				$query5 = "update event_movie_starcast_images set event_id='".$data['event_id']."',starcast_name='".$data["starcast_image"]['title'][$id]."',starcast_image_name='".$this->conn->baseurl().$starcast_image_path."',starcast_image_title='".$filename."',updated_date='".date("Y-m-d h:i:s")."' where movie_starcast_image_id='".$movie_starcast_image_ids[$id]."'";
				//print_r($query5);exit;
				$result = $this->conn->dbCommand($query5);
				}
				else
				{
				$file = $data["starcast_image"]['tmp_name'][$id];
				$filename=time().$filename;
				$starcast_image_path="uploads/events/".$filename;
				move_uploaded_file($filetmpname, $movie_image_path);
				$query6 = "insert into event_movie_starcast_images (event_id,starcast_name,starcast_image_name,starcast_image_title,created_date,updated_date) values('".$data['event_id']."','".$this->conn->baseurl().$starcast_image_path."','".$data["starcast_image"]['name'][$id]."','".$filename."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
				//print_r($query6);exit;
				$result = $this->conn->dbCommand($query6);
				}	
				}
				}
				//print_r($data);exit;
				if($data['youtube_remove_id']!="")
				{	
				$youtube_remove_ids = explode(',',$data['youtube_remove_id']);
			 		
				foreach($youtube_remove_ids as $id=>$youtube_remove_id)
				{	
				$query = "delete from event_movie_youtube_videos where movie_youtube_video_id='".$youtube_remove_id."'";
					 //print_r($query);exit;
				$result = $this->conn->dbCommand($query);
			    //print_r($result);exit;
				}
				}
				if($data['youtube_videos']!="")
				{
				
				//$youtube_videos = json_decode($data['youtube_videos']);
				//print_r($youtube_videos);exit;
				foreach($youtube_videos as $id=>$youtube_video)
				{	
				if(isset($youtube_videos[$id]->movie_youtube_video_id) && $youtube_videos[$id]->movie_youtube_video_id!="")
				{
					
			$query1 = "update event_movie_youtube_videos set event_id='".$data['event_id']."',video_name='".$youtube_videos[$id]->video_name."',video_url='".$youtube_videos[$id]->video_url."',video_image_url='".$youtube_videos[$id]->video_image_url."',updated_date='".date("Y-m-d h:i:s")."' where movie_youtube_video_id='".$youtube_videos[$id]->movie_youtube_video_id."'";
			//print_r($query1);exit;
				$result1 = $this->conn->dbCommand($query1);
			}
			else
			{
				$query2 = "insert into event_movie_youtube_videos (event_id,video_name,video_url,video_image_url,created_date,updated_date) values ('".$data['event_id']."','".$youtube_videos[$id]->video_name."','".$youtube_videos[$id]->video_url."','".$youtube_videos[$id]->video_image_url."','".date("Y-m-d h:i:s")."','".date("Y-m-d h:i:s")."')";
				//print_r($query2);exit;
				$result2 = $this->conn->dbCommand($query2);
			}	
			}
			}
			if($result2)
				{
					return array('message'=> 'Successfully Updated');
				}else 
				{
					return array('message'=>'Updation Failed');
				} 
		
	}
}		
	public function delete_event($data)
	 {
		 //print_r($data);exit;
	    $query = "update `events` e left join event_type et on e.event_type_id=et.event_type_id left join event_location_times elt on e.event_id=elt.event_id left join event_pricing ep on elt.event_pricing_id=ep.event_pricing_id left join event_movie_details emd on e.event_id=emd.event_id left join event_location el on elt.location_id=el.location_id left join pricing p on ep.event_pricing_id=p.event_pricing_id left join event_movie_starcast_images sci on e.event_id=sci.event_id left join event_movie_youtube_videos eyv on e.event_id=eyv.event_id left join event_movie_images emi on e.event_id=emi.event_id set e.status='".$data['status']."',et.status='".$data['status']."',ep.status='".$data['status']."',emd.status='".$data['status']."',elt.status='".$data['status']."',el.status='".$data['status']."',p.status='".$data['status']."',sci.status='".$data['status']."',eyv.status='".$data['status']."',emi.status='".$data['status']."' where e.event_id='".$data['event_id']."'";
		//print_r($query);exit;
		$result = $this->conn->dbCommand($query);
		if($result)
		{
			return array('message'=>'Deletion Successfully');
		}else
		{
			return array('message'=>'Deletion Failed');
		}
	}
	public function all_events($data)
	{
	 $current_page=0;
	$results_per_page=10;
	$order_by="created_date";
	$sort_order="desc";
	
		
	if(isset($data['order_by']) && $data['order_by']!='')
	{
		$order_by=$data['order_by'];
	}
	if(isset($data['results_per_page']) && $data['sort_order']!='')
	{
		$sort_order=$data['sort_order'];
	}
	if(isset($data['current_page'])&& $data['current_page']!='' || $data['current_page']!=0)
	{
	$current_page=$data['current_page'];
	}
	if(isset($data['results_per_page']) && $data['results_per_page']!='' || $data['results_per_page']!=0){
	$results_per_page=$data['results_per_page'];
	}
	$offset=$current_page;
	$query_string="select * from `events`";
	$query_string1="SELECT count(*) as total from `events`";
	if (isset($data['search_by']) && $data['search_by']!='')
	{	
		if($data['search_by']=='status')
		{	
			$search_by = "status";
		}
		else if($data['search_by'] == 'event_title')
		{
			$search_by = "event_title";
		}
		$query =$query_string." where ".$search_by." like '%".$data['search_by_value']."%' order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
		$query1 =$query_string1." where ".$search_by." like '%".$data['search_by_value']."%'";
		 
	}else
	{
	$query = $query_string." order by ".$order_by." ".$sort_order." limit ".$results_per_page." offset ".$offset;
	$query1 = $query_string1;
	}	
		//return $query;exit;
		$result=$this->conn->dbMultiple($query);
		if($result){
			$totalrows = $this->conn->dbSingle($query1);
		return array('message'=>"All Events","info"=>$result,'total'=>$totalrows['total'],'current_page'=>$current_page,'results_per_page'=>$results_per_page);
		}
		else
		return array('message'=>'No Events');
	}
	public function get_association_list()
	{
		$query = "select association_id,association_name from association"; 
		$result = $this->conn->dbMultiple($query);
		if($result)
		{
			return array('message'=>'All associations','result'=>$result);
		}else
		{
			return array('message'=>'no associations');
		}
		
	}
}
	
?>